<template>
  <div>
    <div style="margin: 1px auto;width: 1528px;margin-bottom: 15px">
      <el-button type="primary" @click="handleAdd"> 新 增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:9091/word/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
      </el-upload>
      <el-button type="primary" class="ml-5" @click="exp">导出 <i class="el-icon-top"></i></el-button>
      <el-input style="width: 200px" class="ml-5" placeholder="word" suffix-icon="el-icon-search" v-model="word"></el-input>
      <el-input style="width: 200px" placeholder="mean" suffix-icon="el-icon-position" class="ml-5" v-model="mean"></el-input>
      <el-select v-model="part" placeholder="根据词性查询" class="ml-5">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          <span style="float: left">{{ item.label }}</span>
          <span style="float: right; color: #86a8d9; font-size: 13px">{{ item.value }}</span>
        </el-option>
      </el-select>
      <el-select v-model="gride" placeholder="分类显示" class="ml-5">
        <el-option
            v-for="item in grides"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          <span style="float: left">{{ item.label }}</span>
          <span style="float: right; color: #86a8d9; font-size: 13px">{{ item.value }}</span>
        </el-option>
      </el-select>
      <el-select v-model="unit" placeholder="单元分类" class="ml-5">
        <el-option
            v-for="item in units"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          <span style="float: left">{{ item.label }}</span>
          <span style="float: right; color: #86a8d9; font-size: 13px">{{ item.value }}</span>
        </el-option>
      </el-select>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>

    </div>


    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" ></el-table-column>
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="word" label="word" width="200" align="center" ></el-table-column>
      <el-table-column prop="part" label="part" width="120" align="center"></el-table-column>
      <el-table-column prop="mean" label="mean1" align="center"></el-table-column>
      <el-table-column prop="mean2" label="mean2" align="center"></el-table-column>
      <el-table-column prop="mean3" label="mean3" align="center"></el-table-column>
      <el-table-column prop="gride" label="gride" align="center"></el-table-column>
      <el-table-column prop="unit" label="unit" align="center"></el-table-column>
      <el-table-column prop="know" label="know" align="center" width="120"></el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="新增词汇" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="word">
          <el-input v-model="form.word" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="part" placeholder="请选择词性">
          <el-select v-model="form.part">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="mean1">
          <el-input v-model="form.mean" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="mean2">
          <el-input v-model="form.mean2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="mean3">
          <el-input v-model="form.mean3" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="gride">
          <el-select v-model="form.gride" placeholder="请选择">
            <el-option
                v-for="item in grides"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="unit">
          <el-select v-model="form.unit" placeholder="请选择">
            <el-option
                v-for="item in units"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Word",
  data() {
    return {
      tableData: [],
      total:"",
      pageNum: 1,
      pageSize: 20,
      word: "",
      mean: "",
      mean2:"",
      mean3:"",
      gride:"",
      unit:"",
      part: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      options: [{
        value: 'n(名词)',
        label: '名词'
      }, {
        value: 'V(动词)',
        label: '动词'
      }, {
        value: 'adj(形容词)',
        label: '形容词'
      }, {
        value: 'adv(副词)',
        label: '副词'
      }, {
        value: '介词',
        label: '介词'
      }],
      grides: [{
        value: '必考词',
        label: '必考词'
      }, {
        value: '基础词',
        label: '基础词'
      }, {
        value: '高中词汇',
        label: '高中词汇'
      },{
        value: '四级词汇',
        label: '四级词汇'
      }, {
        value: '六级词汇',
        label: '六级词汇'
      }, {
        value: '超纲词汇',
        label: '超纲词汇'
      }],
      units:[{
        value: 'unit1',
        label: 'unit1'
      },
        {
          value: 'unit2',
          label: 'unit2'
        },
        {
          value: 'unit3',
          label: 'unit3'
        },
        {
          value: 'unit4',
          label: 'unit4'
        },
        {
          value: 'unit5',
          label: 'unit5'
        },
        {
          value: 'unit6',
          label: 'unit6'
        },
        {
          value: 'unit7',
          label: 'unit7'
        },
        {
          value: 'unit8',
          label: 'unit8'
        },
        {
          value: 'unit9',
          label: 'unit9'
        },
        {
          value: 'unit10',
          label: 'unit10'
        },
        {
          value: 'unit11',
          label: 'unit11'
        },
        {
          value: 'unit12',
          label: 'unit12'
        },
        {
          value: 'unit13',
          label: 'unit13'
        },
        {
          value: 'unit14',
          label: 'unit14'
        },
        {
          value: 'unit15',
          label: 'unit15'
        },
        {
          value: 'unit16',
          label: 'unit16'
        },{
          value: 'unit17',
          label: 'unit17'
        },
        {
          value: 'unit18',
          label: 'unit18'
        },
        {
          value: 'unit19',
          label: 'unit19'
        },
        {
          value: 'unit20',
          label: 'unit20'
        },
        {
          value: 'unit21',
          label: 'unit21'
        },
        {
          value: 'unit22',
          label: 'unit22'
        },
        {
          value: 'unit23',
          label: 'unit23'
        },
        {
          value: 'unit24',
          label: 'unit24'
        },
        {
          value: 'unit25',
          label: 'unit25'
        },
        {
          value: 'unit26',
          label: 'unit26'
        },
        {
          value: 'unit27',
          label: 'unit27'
        },
        {
          value: 'unit28',
          label: 'unit28'
        },
        {
          value: 'unit29',
          label: 'unit29'
        },
        {
          value: 'unit30',
          label: 'unit30'
        },
      ]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/word/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          word: this.word,
          part: this.part,
          mean: this.mean,
          mean2: this.mean2,
          mean3: this.mean3,
          unit: this.unit,
          gride: this.gride,
        }
      }).then(res => {
        console.log(res)
        console.log(res.length)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    save() {
      this.request.post("/word", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    del(id) {
      this.request.delete("/word/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/word/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.word = ""
      this.part = ""
      this.mean = ""
      this.mean2 = ""
      this.mean3 = ""
      this.gride = ""
      this.unit=""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    //批量导出
    exp() {
      window.open("http://localhost:9091/word/export")
    },
    //批量导入
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>

<style>

</style>